<?php
/**
 * PHPECS社交电商系统，使用thinkphp框架+MySQL数据库编写的小程序商城系统，基于LGPL协议开源授权
 * @package phpecs
 * @author qq(123865789)
 * @copyright 2018-2020 深圳塔灯网络科技有限公司
 * @version 2.0
 * @license http://www.phpecs.com/lgpl.html phpecs开源授权协议：GNU Lesser General Public License
 **/
namespace app\hgapi\model\system;
use think\Model;
class Message extends Model{

    /**
     * 查出弹窗
     * @param $where
     * @param $field
     * @param string $order
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function selMessageZhi($uid, $start, $limit, $field = '', $order = 'time desc'){
        return self::where('status', 1)->where(function ($query)use($uid){
             $query->where('obj', 1)->whereOr(function ($query)use($uid){
                 $query->where(['obj' => 2, 'user' => $uid]);
             })->whereOr(function ($query)use($uid){
                 $query->where(['obj' => 2, 'user' => ['like', $uid . ',%']]);
             })->whereOr(function ($query)use($uid){
                 $query->where(['obj' => 2, 'user' => ['like', '%,' . $uid . ',%']]);
             })->whereOr(function ($query)use($uid){
                 $query->where(['obj' => 2, 'user' => ['like', '%,' . $uid]]);
             });
        })->field($field)->order($order)->limit($start, $limit)->select();
    }

    /**
     * 统计数量
     * @param $uid
     * @return int|string
     */
    public static function countMessageZhi($uid){
        return self::where('status', 1)->where(function ($query)use($uid){
            $query->where('obj', 1)->whereOr(function ($query)use($uid){
                $query->where(['obj' => 2, 'user' => $uid]);
            })->whereOr(function ($query)use($uid){
                $query->where(['obj' => 2, 'user' => ['like', $uid . ',%']]);
            })->whereOr(function ($query)use($uid){
                $query->where(['obj' => 2, 'user' => ['like', '%,' . $uid . ',%']]);
            })->whereOr(function ($query)use($uid){
                $query->where(['obj' => 2, 'user' => ['like', '%,' . $uid]]);
            });
        })->count();
    }

}